package com.neusoft.neuiotms.dcom.converter;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Joiner;
import com.neusoft.bizcore.web.dto.AbstractConverter;
import com.neusoft.bizcore.web.utils.id.BaseIdGenerator;
import com.neusoft.neuiotms.dcom.constants.DCOMConstants;
import com.neusoft.neuiotms.dcom.dto.CMDBInstanceDTO;
import com.neusoft.neuiotms.dcom.dto.StopServiceNoticeDTO;
import com.neusoft.neuiotms.dcom.dto.UserDTO;
import com.neusoft.neuiotms.dcom.model.StopServiceNotice;
import com.neusoft.neuiotms.dcom.repository.StopServiceNoticeRepository;

@Component
public class StopServiceNoticeConverter extends AbstractConverter<StopServiceNotice, StopServiceNoticeDTO> {

    @Autowired
    private StopServiceNoticeRepository stopServiceRepository;
    @Autowired
    private BaseIdGenerator stopServiceNoticeCodeGenerator;

    @Override
    public StopServiceNotice toModel(final StopServiceNoticeDTO dto) {
        StopServiceNotice model;
        if (dto.isNew()) {
            model = new StopServiceNotice();
            model.setCreateTime(new Date());
        } else {
            model = this.stopServiceRepository.getOne(dto.getId());
            model.setUpdateTime(new Date());
        }
        model.setCode(
                StringUtils.isEmpty(dto.getCode()) ? this.stopServiceNoticeCodeGenerator.generateCode()
                        : dto.getCode());
        model.setName(dto.getName());

        model.setStatus(dto.getStatus());

        model.setOrgCode(dto.getOrgCode());
        model.setOrgName(dto.getOrgName());

        model.setStartDateTime(dto.getStartDateTime());
        model.setEndDateTime(dto.getEndDateTime());

        model.setStopTime(dto.getStopTime());
        model.setLocation(dto.getLocation());

        model.setNoticeType(Joiner.on(DCOMConstants.COMMA_SPILT).join(dto.getNoticeType()));

        model.setContent(dto.getContent());
        model.setImpactSafeguard(dto.getImpactSafeguard());
        model.setOthers(dto.getOthers());

        model.setCreateTime(dto.getCreateTime());
        model.setUpdateTime(dto.getUpdateTime());

        model.setUserId(dto.getUserId());
        model.setUserName(dto.getUserName());

        model.setModelCode(dto.getModelCode());
        model.setModelName(dto.getModelName());

        model.setJsonObjs(dto.getJsonObjs());

        model.setJsonUsers(dto.getJsonUsers());

        return model;
    }

    @Override
    public StopServiceNoticeDTO toDTO(final StopServiceNotice model, final boolean forListView) {
        final StopServiceNoticeDTO dto = new StopServiceNoticeDTO();

        dto.setId(model.getId());

        dto.setCode(model.getCode());
        dto.setName(model.getName());

        dto.setStatus(model.getStatus());

        dto.setOrgCode(model.getOrgCode());
        dto.setOrgName(model.getOrgName());

        dto.setStartDateTime(model.getStartDateTime());
        dto.setEndDateTime(model.getEndDateTime());

        dto.setStopTime(model.getStopTime());

        dto.setLocation(model.getLocation());

        dto.setNoticeType(Arrays.asList(model.getNoticeType().split(DCOMConstants.COMMA_SPILT)));

        dto.setContent(model.getContent());
        dto.setImpactSafeguard(model.getImpactSafeguard());
        dto.setOthers(model.getOthers());

        dto.setCreateTime(model.getCreateTime());
        dto.setUpdateTime(model.getUpdateTime());

        dto.setUserId(model.getUserId());
        dto.setUserName(model.getUserName());

        dto.setModelCode(model.getModelCode());
        dto.setModelName(model.getModelName());

        if (StringUtils.isNotEmpty(model.getJsonObjs())) {
            dto.setJsonObjs(model.getJsonObjs());

            final List<CMDBInstanceDTO> iList = JSON.parseArray(model.getJsonObjs(), CMDBInstanceDTO.class);
            if (CollectionUtils.isNotEmpty(iList)) {
                final List<String> nameList =
                        iList.stream().map(CMDBInstanceDTO::getName).collect(Collectors.toList());
                dto.setObjNames(Joiner.on(DCOMConstants.COMMA_SPILT).join(nameList));
            }

        }

        if (StringUtils.isNotEmpty(model.getJsonUsers())) {
            dto.setJsonUsers(model.getJsonUsers());

            final List<UserDTO> uList = JSON.parseArray(model.getJsonUsers(), UserDTO.class);
            if (CollectionUtils.isNotEmpty(uList)) {
                dto.setUsers(uList);

                final List<String> unameList =
                        uList.stream().map(UserDTO::getName).collect(Collectors.toList());
                dto.setUserNames(Joiner.on(DCOMConstants.COMMA_SPILT).join(unameList));
            }

        }

        return dto;
    }

}
